package com.example.demo.controller;

import com.example.demo.pojo.Chose;
import com.example.demo.service.IChoseService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@Controller
@RequestMapping("/Chose")
public class ChoseController {

    @Autowired
    private IChoseService ChoseService;

    @RequestMapping("/selectChose")
    public void selectChose(HttpServletResponse response) throws IOException {
        List<Chose> list=ChoseService.selectChose();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectChoseByTid")
    public void selectChoseByTid(HttpServletResponse response,Integer tid) throws IOException {
        List<Chose> list=ChoseService.selectChoseByTid(tid);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/addChose")
    private void addChose(HttpServletResponse response,Chose Chose, String answer, Integer num, Integer tid) throws IOException {
        Chose.setAnswer(answer);
        Chose.setNum(num);
        Chose.setTid(tid);
        ChoseService.insertSelective(Chose);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/toupdateChose")
    private void toupdateChose(HttpServletResponse response, Chose Chose,Integer id) throws IOException {
        Gson gson = new Gson();
        String json = gson.toJson(ChoseService.selectByPrimaryKey(id));
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/updateChose")
    private void updateChose(HttpServletResponse response, Chose Chose, String answer, Integer num, Integer id) throws IOException {
        Chose.setAnswer(answer);
        Chose.setNum(num);
        Chose.setId(id);
        ChoseService.updateByPrimaryKeySelective(Chose);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/deleteChose")
    private void deleteChose(HttpServletResponse response,Chose Chose, Integer id) throws IOException {
        ChoseService.deleteByPrimaryKey(id);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }
}
